//
//  IcAppViewController.m
//  IceClear
//
//  Created by P & C on 2/23/11.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "IcAppViewController.h"
#import "IcMaterialsChartViewController.h"
#import "IcAntiIcingTipsViewController.h"
#import "IcDeIcingTipsViewController.h"
#import "IcRecommendationsListViewController.h"
#import "IcMaterialsListViewController.h"


@implementation IcAppViewController

@synthesize mainTabBar;
@synthesize appModelsManager;


// The designated initializer.  Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
/*
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization.
    }
    return self;
}
*/

// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];
	
	// This is where our application's view related global initialization occurs
	
	// first create the view controllers that serve as tab contents
	IcMaterialsChartViewController* vcMatsChart = [[IcMaterialsChartViewController alloc] 
												   initWithNibName:@"IcMaterialsChartViewController" bundle:nil];
	IcAntiIcingTipsViewController* vcATips = [[IcAntiIcingTipsViewController alloc] 
											  initWithNibName:@"IcAntiIcingTipsViewController" bundle:nil];
	IcDeIcingTipsViewController* vcDTips = [[IcDeIcingTipsViewController alloc] 
											  initWithNibName:@"IcDeIcingTipsViewController" bundle:nil];
	IcRecommendationsListViewController* vcRecsList = [[IcRecommendationsListViewController alloc] 
													initWithNibName:@"IcRecommendationsListViewController" bundle:nil];
	IcMaterialsListViewController* vcMatsList = [[IcMaterialsListViewController alloc] 
												 initWithNibName:@"IcMaterialsListViewController" bundle:nil];
	
	// customize the top view controllers of each tab
	// by giving them the models they require to function
	// and a reference to our main models manager
	vcMatsChart.appModelsManager = self.appModelsManager;
	vcMatsList.title = @"Practical Effective Temperatures";
	
	vcATips.appModelsManager = self.appModelsManager;
	vcDTips.appModelsManager = self.appModelsManager;
	
	vcRecsList.title = @"My Recommendations";
	vcRecsList.appModelsManager = self.appModelsManager;
	vcRecsList.recommendationArray = self.appModelsManager.recommendationsManager.recommendationArray;
	
	vcMatsList.title = @"Materials";
	vcMatsList.appModelsManager = self.appModelsManager;
	vcMatsList.materials = [self.appModelsManager.materialsManager allMaterialsGrouped];
	
	// wrap some of the top view controllers with navigation controllers
	UINavigationController* recsTabNavController = [[UINavigationController alloc] initWithRootViewController:vcRecsList];
	UINavigationController* matsTabNavController = [[UINavigationController alloc] initWithRootViewController:vcMatsList];
	
	// customize the icons of each tab
	vcMatsChart.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"" image:[UIImage imageNamed:@"bar_chart_24.png"] tag:0];
	vcATips.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"" image:[UIImage imageNamed:@"ice_24.png"] tag:0];
	vcDTips.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"" image:[UIImage imageNamed:@"round_ice_24.png"] tag:0];
	recsTabNavController.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"" 
																	image:[UIImage imageNamed:@"article_24.png"] tag:0];
	vcMatsList.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"" 
																	image:[UIImage imageNamed:@"materials_24.png"] tag:0];
	
	// pass our tab content view controllers to the main tab bar
	self.mainTabBar.viewControllers = [NSArray arrayWithObjects:vcATips, vcDTips, recsTabNavController, matsTabNavController, 
									   vcMatsChart, nil];
	
	[self.view addSubview:mainTabBar.view];
}

/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations.
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/

- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc. that aren't in use.
}

- (void)viewDidUnload {
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}


#pragma mark tabBarController delegation

- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController {
	return YES;
}

- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController {
	
}


- (void)dealloc {
	[mainTabBar release];
	[appModelsManager release];
	[super dealloc];
}


@end
